package com.im.project.controller;

import com.im.common.result.JsonResult;
import com.im.project.entity.dto.*;
import com.im.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户 控制层
 *
 * @author yunZhouJiaDi
 */
@RestController
@RequestMapping("/user")
@Api(tags = "UserController", description = "用户相关接口")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/code")
    @ApiOperation("邮箱验证码")
    public JsonResult sendMailCode(@Validated @RequestBody SendMailCodeDto sendMailCodeDto) {
        return JsonResult.success(userService.sendMailCode(sendMailCodeDto));
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public JsonResult register(@Validated @RequestBody RegisterDto registerDto) {
        return JsonResult.success(userService.register(registerDto));
    }

    @GetMapping("/captcha")
    @ApiOperation("生成验证码")
    public JsonResult captcha(HttpServletResponse response) {
        return JsonResult.success(userService.captcha(response));
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public JsonResult login(@Validated @RequestBody LoginDto loginDto) {
        return JsonResult.success(userService.login(loginDto));
    }

    @PutMapping("/resetPassword")
    @ApiOperation("重置密码")
    public JsonResult ResetPassword(@Validated @RequestBody ResetPasswordDto resetPasswordDto) {
        return JsonResult.success(userService.resetPassword(resetPasswordDto));
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public JsonResult logout() {
        return JsonResult.success(userService.logout());
    }

    @GetMapping("/info")
    @ApiOperation("当前登录用户信息")
    public JsonResult queryCurrentUserDetail() {
        return JsonResult.success(userService.queryCurrentUserDetail());
    }

    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public JsonResult updateCurrentUser(@Validated @RequestBody UpdateUserDto updateUserDto) {
        return JsonResult.success(userService.updateCurrentUser(updateUserDto));
    }

    @PostMapping("/update/password")
    @ApiOperation("修改用户密码")
    public JsonResult updateUserPassword(@Validated @RequestBody UpdateUserPasswordDto updateUserPasswordDto) {
        return JsonResult.success(userService.updateUserPassword(updateUserPasswordDto));
    }

}
